package com.plan.dao.service.user.impl;

import com.plan.controller.user.response.UserInitData;
import com.plan.dao.mapper.card.CardMapper;
import com.plan.dao.mapper.card.DayPlanMapper;
import com.plan.dao.mapper.card.DayPlanStatMapper;
import com.plan.dao.mapper.card.PointLogMapper;
import com.plan.dao.mapper.card.TeamMapper;
import com.plan.dao.mapper.user.TeamUserMapper;
import com.plan.dao.mapper.user.UserMapper;
import com.plan.dao.mapper.user.UserPackageMapper;
import com.plan.dao.service.user.UserTransactionService;
import com.plan.entity.card.Card;
import com.plan.entity.card.DayPlan;
import com.plan.entity.card.DayPlanStat;
import com.plan.entity.card.PointLog;
import com.plan.entity.card.Team;
import com.plan.entity.card.TeamUser;
import com.plan.entity.user.User;
import com.plan.entity.user.UserPackage;
import com.plan.enums.DelFlagEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserTransactionServiceImpl implements UserTransactionService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private TeamMapper teamMapper;
    @Resource
    private PointLogMapper pointLogMapper;
    @Resource
    private DayPlanMapper dayPlanMapper;
    @Resource
    private DayPlanStatMapper dayPlanStatMapper;
    @Resource
    private UserPackageMapper userPackageMapper;
    @Resource
    private TeamUserMapper teamUserMapper;
    @Resource
    private CardMapper cardMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(User user) {
        userMapper.update(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void registy(User user, UserInitData initData, List<UserPackage> userPackages) {
        Team team = initData.getTeam();
        team.setCreatorId(0L);
        team.setUpdaterId(0L);
        teamMapper.insert(team);

        user.setFamilyId(team.getId());
        userMapper.insert(user);

        TeamUser teamUser = new TeamUser();
        teamUser.setUserId(user.getId());
        teamUser.setTeamId(team.getId());
        teamUser.setCreatorId(user.getId());
        teamUser.setUpdaterId(user.getId());
        teamUser.setDelFlag(DelFlagEnum.NORMAL.getType());
        teamUserMapper.insert(teamUser);

        Card card = initData.getCard();
        card.setCreatorId(user.getId());
        card.setUpdaterId(user.getId());
        card.setTeamId(team.getId());
        cardMapper.insert(card);

        List<DayPlanStat> statList = initData.getDayPlanStats();
        if (!CollectionUtils.isEmpty(statList)) {
            for (DayPlanStat s : statList) {
                s.setTeamId(team.getId());
                s.setCardId(card.getId());
            }
            dayPlanStatMapper.batchInsert(statList);
        }
        List<DayPlan> dayPlanList = initData.getDayPlanList();
        if (!CollectionUtils.isEmpty(dayPlanList)) {
            for (DayPlan p : dayPlanList) {
                p.setTeamId(team.getId());
                p.setCardId(card.getId());
                p.setUpdaterId(user.getId());
                p.setCreatorId(user.getId());
            }
            dayPlanMapper.batchInsert(dayPlanList);
        }

        List<PointLog> pointLogs = initData.getPointLogs();
        if (!CollectionUtils.isEmpty(pointLogs)) {
            for (PointLog l : pointLogs) {
                l.setTeamId(team.getId());
                l.setCardId(team.getId());
                l.setCreatorId(user.getId());
                l.setCreatorId(user.getId());
            }
            pointLogMapper.batchInsert(pointLogs);
        }

        if (!CollectionUtils.isEmpty(userPackages)) {
            for (UserPackage item : userPackages) {
                item.setUserId(user.getId());
            }
            userPackageMapper.batchInsert(userPackages);
        }
    }
}
